<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="add">add</button>
    <button id="remove">remove</button>
    <div id="content"></div>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(function () {
            var dfd = new $.Deferred();
            var add = $("#add");
            var remove = $("#remove");
            var content = $("#content");
            add.click(function () {
                var span = $("<span>我是点击按钮创建的元素</span>");
                $("span").length == 0 && $("body").append(span);
                dfd.resolve();
            })
            remove.click(function () {
                var span = $("span");
                span && span.remove();
            })
            dfd.done(function () {
                $("span").css("color", "red");
            })
        })
    </script>
</body>

</html>